define(['jquery', 'bootstrap'], function ($) {
	/**
	 * 搜索相关信息
	 * @params opt => 先关配置项
	 * @opt.callback =>  
	 */
	function SearchData (opt) {
		this.opt = {
			elem: '.serch-cont', // 点击搜索的input
			url: '',
			project_name: '', // 项目id的input的name
			ajax_data_elem: [], // 额外需要传递的数据的input, [{name: '', msg: '提示的消息'}],
			ajax_data: {},
			modal_elem: $('.modal'), // 对应的模态框
			confirm: '', // 模态框确认按钮
			search_btn: '.search-btn', // 模态框内部搜索按钮
			name: '', // ajax返回数据的名称, {code， msg, nmae: [{}]},
			condition: [], // 过滤的调价，暂未使用
			callback: '', // Function 必填, 处理ajax获取的数据， 返回一段html代码， <tr>...</tr>
			success: '', // Function 处理选中的搜索结果，
			content: true, // 是否需要传递countent
		};

		this.result; // 点击elem 请求到的全部数据

		this.filter_result; // 根据模态框内输入的搜索条件过滤出的结果， 默认是this.result

		this.opt = $.extend({}, this.opt, opt);

		this.Init();
	}

	/**
	 * 对象实例化后立即执行
	 * 完成事件绑定
	 */
	SearchData.prototype.Init = function () {
	  this.elemBindClick();
	  this.searchBindClick();
	  this.confirmBindClick();
	}


	/*
	 * ajax
	 * @params: url=> ajax请求地址， data=> ajax提交数据
	 */
	SearchData.prototype.ajaxPost = function () {

		var data = this.opt.content ? {content: ''} : {},
			_self = this;

		// 根据project_name 判断是否需要传递项目id
		if (_self.opt.project_name) data.project_id = $('select[name="'+ _self.opt.project_name +'"]').val();

		return new Promise(function (resolve, reject) {
			// 判断是否有额外需要传递的数据
			var ajax_elems = _self.opt.ajax_data_elem;

			if (ajax_elems.length > 0) {
				for (var i = 0; i < ajax_elems.length; i++) {
					var val = $('[name="'+ajax_elems[i].name +'"]').val();
					if (val == '') {
						return alert(ajax_elems[i].msg);
					}
					data[ajax_elems[i].name] = val;
				}
				
			}

			data = $.extend(data, _self.opt.ajax_data);
			console.log(_self.opt.ajax_data)

			$.ajax({
				type: 'POST',
				url: _self.opt.url,
				data: data,
				dataType: 'json',
				timeout: 10000,
				success: function (json) {
					json.code == 1 ? resolve(json) : alert(json.msg);
				},
				error: function () {
					alert('获取信息失败');
				} 
			});
		});
	}

	/**
	 * elem 绑定点击事件， 搜索信息
	 * 
	 */
	SearchData.prototype.elemBindClick = function () {

		var _self = this;

		$(document).on('click', _self.opt.elem, function () {

			// if (_self.result) return _self.opt.modal_elem.modal();

			_self.ajaxPost().then(function (json) {
				_self.opt.modal_elem.modal();
				_self.result = json[_self.opt.name];
				_self.filter_result = _self.result;
				var str = _self.opt.callback(_self.filter_result);
				_self.opt.modal_elem.find('tbody').html(str);
			});

		});
	}

	/**
	 * search_btn 绑定click事件， 模态框内部搜索
	 * 
	 */

	SearchData.prototype.searchBindClick = function () {

		var _self = this;

		_self.opt.modal_elem.on('click', _self.opt.search_btn, function () {

			var condition = $(this).parent().siblings('input').val();
			_self.filterData(condition);
			console.log(_self.filter_result);
			var str = _self.opt.callback(_self.filter_result);
			_self.opt.modal_elem.find('tbody').html(str);

		});



		// 当搜索内容为空时显示全部搜索结果
		$(_self.opt.modal_elem).find(_self.opt.search_btn).parent().siblings('input').on('input', function () {console.log(_self.result);
			if (this.value == '') {
				var str = _self.opt.callback(_self.result);
				_self.opt.modal_elem.find('tbody').html(str);
				_self.filter_result = _self.result;
			}
		});
	}


	/**
	 * 根据搜索条件过滤数据
	 * @params: cond => 过滤条件
	 */
	SearchData.prototype.filterData = function (cond) {

		var reg = new RegExp(cond),
			_self = this;

		_self.filter_result = [];

		$.each(_self.result, function (n, val) {

			for (var name in val) {
				
				if (reg.test(val[name])) {
					return _self.filter_result.push(val); 
				}
			}

		});
	}

	/**
	 * 模态框确认按钮click事件绑定
	 * 
	 */	
	SearchData.prototype.confirmBindClick = function () {

		var _self = this;

		$( _self.opt.confirm).on('click', function () {

			var selected = _self.opt.modal_elem.find('input:checked');

			if (selected.length <= 0) return;

			var selected_result = [],
				ind;



			// checkbox 多选
			if (selected.length > 1) {
				selected.each(function (n, val) {
					ind = $(val).data('ind');
					selected_result.push(_self.filter_result[ind]);
				});
			} 
			// radio 单选
			else {
				ind = selected.val();
				selected_result.push(_self.filter_result[ind]);
			}
			

			_self.opt.modal_elem.modal('hide');

			_self.opt.success(selected_result);
		});
	} 


	return SearchData;

});